package com.example.core.service;


import cn.hutool.core.util.RuntimeUtil;
import com.example.core.po.SysLicenseParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/***
 *
 * 生成对应证书的操作
 */
@Service
public class SysKeyStoreServiceImpl implements SysKeyStoreService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${sitc.path}")
    private String dirPath;

    @Override
    public void create(SysLicenseParam sysLicenseParam) throws Exception {
        this.createPrivateKeys(sysLicenseParam);
        this.exportPrivateKeys(sysLicenseParam);
        this.importPublicCert(sysLicenseParam);
    }

    @Override
    public void createPrivateKeys(String alias, String subject, String privateKey, String storepass, String keypass) throws Exception {
        String shell = "keytool -genkey -alias " + alias + " -keystore " + dirPath + privateKey + ".keystore -keysize 1024 -validity 3650 -dname \"CN=, OU=" + subject + ", O=, L=, S=ShangHai, C=CN\" -storepass " + storepass + " -keypass " + keypass;
        int wait = RuntimeUtil.exec(shell).waitFor();
        if (wait == 0) {
            logger.warn("SysKeyStoreServiceImpl createPrivateKeys 正常结束...");
        }
    }

    @Override
    public void createPrivateKeys(SysLicenseParam sysLicenseParam) throws Exception {
        this.createPrivateKeys(sysLicenseParam.getPrivateAlias(), sysLicenseParam.getSubject(), sysLicenseParam.getPrivateKey(), sysLicenseParam.getStorePwd(), sysLicenseParam.getKeyPwd());
    }

    @Override
    public void exportPrivateKeys(String alias, String certfile, String privateKey, String storepass) throws Exception {
        String shell = "keytool -export -noprompt -alias " + alias + " -file " + dirPath + certfile + ".cer -keystore " + dirPath + privateKey + ".keystore -storepass " + storepass;
        int wait = RuntimeUtil.exec(shell).waitFor();
        if (wait == 0) {
            logger.warn("SysKeyStoreServiceImpl exportPrivateKeys 正常结束...");
        }
    }

    @Override
    public void exportPrivateKeys(SysLicenseParam sysLicenseParam) throws Exception {
        this.exportPrivateKeys(sysLicenseParam.getPrivateAlias(), sysLicenseParam.getCertfile(), sysLicenseParam.getPrivateKey(), sysLicenseParam.getStorePwd());
    }

    @Override
    public void importPublicCert(String alias, String certfile, String publicKey, String storepass) throws Exception {
        String shell = "keytool -import -noprompt -alias " + alias + " -file " + dirPath + certfile + ".cer -keystore " + dirPath + publicKey + ".keystore -storepass " + storepass;
        int wait = RuntimeUtil.exec(shell).waitFor();
        if (wait == 0) {
            logger.warn("SysKeyStoreServiceImpl importPublicCert 正常结束...");
        }
    }

    @Override
    public void importPublicCert(SysLicenseParam sysLicenseParam) throws Exception {
        this.importPublicCert(sysLicenseParam.getPrivateAlias(), sysLicenseParam.getCertfile(), sysLicenseParam.getPublicKey(), sysLicenseParam.getStorePwd());
    }
}

